package com.aier.user.service.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.aier.common.common.Constant;
import com.aier.common.dao.ICommonDAO;
import com.aier.user.entity.Follow;
import com.aier.user.entity.mapper.FollowRowMapper;
import com.aier.user.entity.mapper.FriendRowMapper;
import com.aier.user.service.ICloseFriendService;

@Component("closeFriendService")
public class CloseFriendServiceImpl_J implements ICloseFriendService {

	@Resource(name = "commonDao")
	private ICommonDAO commonDao;

	public void setCommonDao(ICommonDAO commonDao) {
		this.commonDao = commonDao;
	}

	@Override
	public int insertFriend(Long uid, Long auid, String content) {
		// 已存在该密友
		if (null != commonDao.listByObject(
				"select * from friend where uid = ? and auid = ?",
				new Object[] { uid, auid }, new FriendRowMapper())) {
			// 修改对方的密友状态（等待确认中--密友）
			commonDao.update(
					"update friend set state =? where uid = ? and auid = ?",
					new Object[] { 1, auid, uid });
			return Constant.num_10001;
		}
		String nickname = (String) commonDao.listByObject(
				"select nickname from user where uid=?", new Object[] { uid },
				new RowMapper<String>() {
					@Override
					public String mapRow(ResultSet rs, int arg1)
							throws SQLException {
						return rs.getString("nickname");
					}
				});
		if (content == null) {
			content = "";
		}
		if (null != commonDao.listByObject(
				"select * from friend where uid = ? and auid = ?",
				new Object[] { auid, uid }, new FriendRowMapper())) {
			// 对方已加你为密友
			// 如果关注中没有，添加关注,关注状态为互相关注
			if (null != commonDao.listByObject(
					"select * from follow where uid = ? and fuid = ?",
					new Object[] { uid, auid }, new FollowRowMapper())) {
				commonDao
						.update("update follow set state = 2 where uid = ? and fuid = ?",
								new Object[] { uid, auid });
			} else {
				commonDao
						.update("insert into follow(uid,fuid,fgid,state,remark,cdate) values(?,?,?,?,?,?)",
								new Object[] { uid, auid, -1, 2, null,
										new Timestamp(new Date().getTime()) });
			}
			commonDao.update(
					"update follow set state = 2 where uid = ? and fuid = ?",
					new Object[] { auid, uid });
			// 添加密友
			commonDao
					.update("insert into friend(uid,auid,state,remark,cdate) values(?,?,?,?,?)",
							new Object[] { uid, auid, 1, null,
									new Timestamp(new Date().getTime()) });

			// 修改对方的密友状态（等待确认中--密友）
			commonDao.update(
					"update friend set state =? where uid = ? and auid = ?",
					new Object[] { 1, auid, uid });
			// 发私信
			commonDao
					.update("insert into letter(suid,ruid,type,content,state,sdel,rdel,img,cdate) values(?,?,?,?,?,?,?,?,?)",
							new Object[] {
									uid,
									auid,
									Constant.num_3,
									"你好，@" + nickname + " 已经接受了你的密友邀请，你们已成为密友。"
											+ content, 0, 0, 0, null,
									new Timestamp(new Date().getTime()) });
			commonDao
					.update("update letter set state = ? where ruid = ? and suid = ? and type = ?",
							new Object[] { 1, uid, auid, 2 });
		} else {
			commonDao
					.update("insert into friend(uid,auid,state,remark,cdate) values(?,?,?,?,?)",
							new Object[] { uid, auid, 2, null,
									new Timestamp(new Date().getTime()) });
			// 发私信
			commonDao
					.update("insert into letter(suid,ruid,type,content,state,sdel,rdel,img,cdate) values(?,?,?,?,?,?,?,?,?)",
							new Object[] { uid, auid, 2,
									"你好，@" + nickname + " 申请成为您的密友。" + content,
									0, 0, 0, null,
									new Timestamp(new Date().getTime()) });
		}

		return 1;
	}

	@Override
	public int updateRemark(Long uid, Long auid, String remark, Integer type) {
		if (type == 3) {
			commonDao
					.update("update follow set remark=? where f.fuid = ? and f.uid=? and f.state = 1 ",
							new Object[] { remark, uid, auid });
		} else if (type == 1 || type == 2) {
			commonDao
					.update("update follow f set remark=? where f.uid = ? and f.fuid=? and ( f.state = 1 or f.state =2)",
							new Object[] { remark, uid, auid });
		}
		return 1;
	}

	@Override
	public int deleteFriend(Long uid, String auidStr, Short flag) {
		if (null != auidStr && !"".equals(auidStr)) {
			for (String auid : auidStr.split(",")) {
				commonDao.update(
						"delete from friend where uid = ? and auid = ?",
						new Object[] { uid, auid });
				if (flag == 1) {
					commonDao.update(
							"delete from follow where uid = ? and fuid = ?",
							new Object[] { uid, auid });
					commonDao
							.update("update follow set state = 1 where uid=? and fuid=?",
									new Object[] { auid, uid });
				}
			}
		}
		return 1;
	}

	@Override
	public Map<String, Object> searchAllFriend(int page, int pagesize, Long uid) {

		return commonDao
				.queryPage(
						"select f.*,u.nickname,u.headimg,u.desinfo,fo.fgid,fg.`name` as fgname,fo.state as fstate from friend f "
								+ "left join `user` u on u.uid = f.auid left join follow fo on fo.fuid = f.auid left join follow_group fg "
								+ "on fg.fgid = fo.fgid where f.uid = ? and fo.uid = ?",
						page, pagesize, new Object[] { uid, uid },
						new FriendRowMapper(FriendRowMapper.FRIEND_BRIEF));

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Follow> searchFollowNotFriend(Long uid) {
		return (List<Follow>) commonDao
				.list("select fo.*,u.nickname,u.desinfo,u.headimg from follow fo LEFT JOIN `user` u "
						+ "on u.uid = fo.fuid where fo.uid = ? and fo.fuid not IN (SELECT auid from friend WHERE uid = ?)",
						new Object[] { uid, uid }, new FollowRowMapper());
	}

}
